from datetime import datetime, timedelta

from sqlalchemy import *
from sqlalchemy.ext.activemapper import ActiveMapper, column, \
                                    one_to_many, one_to_one, many_to_many

from turbogears.database import metadata, session

class Entry(ActiveMapper):
    class mapping:
        __table__ = "ffirehose_entries"
        id = column(String(36), primary_key=True)

        channel_entries = one_to_many("ChannelEntry", backref="entry")
        tags = one_to_many("EntryTag", backref="entry")

class ChannelEntry(ActiveMapper):
    class mapping:
        __table__ = "ffirehose_channel_entries"
        channel_id = column(String(36), primary_key=True)
        entry_id = column(String(36), foreign_key=ForeignKey('ffirehose_entries.id'), primary_key=True)
        title = column(Unicode(255))                       # Original title, usually
        contents = column(Unicode())                    # For example, the images?
        commentary = column(Unicode())                # The "Woah, that's cool" for links
        published = column(DateTime())                 # Copied from Entry for simple queries
        updated = column(DateTime())                     # Copied from Entry for simple queries
        url = column(String(255))                            # Copied from Entry for simple queries
        service_id = column(String(36))               # Copied from Entry for simple queries
        service_url = column(String(255))            # Copied from Entry for simple queries
        service_icon_url = column(String(255))   # Copied from Entry for simple queries
        custom_commentary = column(Boolean)    # A human changed the commentary...
        selected = column(Boolean)                      # Whether to display/output this entry
        custom_selected = column(Boolean)        # A human changed the selected state...
        #entry = relation("Entry", backref="channel_entries")

class EntryTag(ActiveMapper):
    class mapping:
        __table__ = "ffirehose_entry_tags"
        entry_id = column(String(36), foreign_key=ForeignKey('ffirehose_entries.id'), primary_key=True)
        tag = column(String(36), primary_key=True)
